package com.jhj.service.resume.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jhj.po.resume.mapper.ResumeMapper;
import com.jhj.po.resume.model.Dict;
import com.jhj.po.resume.model.Resume;
import com.jhj.po.resume.model.ResumeCert;
import com.jhj.po.resume.model.ResumeDetail;
import com.jhj.po.resume.model.ResumeFrom;
import com.jhj.po.resume.model.ResumeResource;
import com.jhj.po.resume.model.ResumeServiceArea;
import com.jhj.po.resume.model.ResumeSkill;
import com.jhj.utils.Constants;
import com.jhj.utils.ResumeUtil;
import com.jhj.utils.StringUtil;
import com.jhj.vo.resume.ResumeDetailVo;
import com.jhj.vo.resume.ResumeListVo;
import com.jhj.vo.resume.ResumeSearchVo;
import com.jhj.vo.resume.ResumeServiceAreaVo;
import com.jhj.vo.resume.ResumeSkillVo;
import com.jhj.service.impl.BaseServiceImpl;
import com.jhj.service.resume.DictService;
import com.jhj.service.resume.ResumeCertService;
import com.jhj.service.resume.ResumeDetailService;
import com.jhj.service.resume.ResumeFromService;
import com.jhj.service.resume.ResumeResourceService;
import com.jhj.service.resume.ResumeService;
import com.jhj.service.resume.ResumeServiceAreaService;
import com.jhj.service.resume.ResumeSkillService;
import com.jhj.utils.BeanUtilsExp;
import com.jhj.utils.DateUtil;
import com.jhj.utils.TimeStampUtil;

/**
 * ResumeService实现类
 */
@Service
public class ResumeServiceImpl extends BaseServiceImpl<Resume> implements ResumeService {

	@Autowired
	ResumeResourceService resumeResourceService;

	@Autowired
	ResumeCertService resumeCertService;

	@Autowired
	ResumeSkillService resumeSkillService;

	@Autowired
	ResumeFromService resumeFromService;

	@Autowired
	ResumeServiceAreaService resumeServiceAreaService;

	@Autowired
	DictService dictService;

	@Autowired
	protected ResumeMapper resumeMapper;
	
	@Autowired
	ResumeDetailService resumeDetailService;

	@SuppressWarnings({ "rawtypes" })
	@Override
	public PageInfo selectByListPage(ResumeSearchVo searchVo, int pageNo, int pageSize, String orderBy) {
		PageHelper.startPage(pageNo, pageSize);

		// 排序
		if (!StringUtil.isEmpty(orderBy)) {
			searchVo.setOrderBy(orderBy);
		}

		List<Resume> list = resumeMapper.selectByListPage(searchVo);
		PageInfo<Resume> pageInfo = new PageInfo<Resume>(list);
		return pageInfo;
	}
	
	@Override
	public Integer statNumBySearchVo(ResumeSearchVo searchVo) {
		return resumeMapper.selectCountBySearchVo(searchVo);
	}
	
	@Override
	public ResumeListVo getListVo(Resume item) {
		ResumeListVo vo = new ResumeListVo();
		BeanUtilsExp.copyPropertiesIgnoreNull(item, vo);
		Integer resumeId = vo.getResumeId();

		// 刷新时间格式化
		Integer refreshTime = vo.getRefreshTime();
		Long nowTime = TimeStampUtil.getNowSecond();
		Long lastTime = nowTime - Long.valueOf(refreshTime);
		String refreshTimeFormat = DateUtil.formatDateTimeBefore(lastTime);
		vo.setRefreshTimeFormat(refreshTimeFormat);

		// 性别名称
		String sexName = ResumeUtil.getSexName(vo.getSex());
		vo.setSexName(sexName);

		// 头像
		String headImg = Constants.DEFAULT_HEAD_IMG;
		ResumeResource resSearchVo = new ResumeResource();
		resSearchVo.setResumeId(resumeId);
		resSearchVo.setResourceType(Constants.RES_RESUME_HEAD_IMG);
		List<ResumeResource> resHeadImgList = resumeResourceService.selectBySearchVo(resSearchVo, "add_time desc");
		if (resHeadImgList != null && resHeadImgList.size()>0) {
			headImg = resHeadImgList.get(0).getResourceUrl();
		}
		vo.setHeadImg(headImg);

		// 工作年限名称
		String workYearName = dictService.getDictName(vo.getWorkYear());
		vo.setWorkYearName(workYearName);

		// 期望工资名称
		String hopeSalaryName = dictService.getDictName(vo.getHopeSalary());
		vo.setHopeSalaryName(hopeSalaryName);
		
		//学历名称 
		String degreeName = dictService.getDictName(vo.getDegreeId());
		vo.setDegreeName(degreeName);

		// 户口省份
		String provinceName = dictService.getDictName(vo.getProvinceId());
		vo.setProvinceName(provinceName);

		// 户口城市
		String cityName = dictService.getDictName(vo.getCityId());
		vo.setCityName(cityName);
		
		//主要服务技能.
		ResumeSkill resumeSkill = new ResumeSkill();
		Integer serviceTypeId = 0;
		String serviceTypeName = "";
		// 服务类型名称
		String serviceTypeNames = "";
		String serviceTypeIds = "";
		ResumeSkill resumeSkillSearchVo = new ResumeSkill();
		resumeSkillSearchVo.setResumeId(resumeId);
		List<ResumeSkill> resumeSkills = resumeSkillService.selectBySearchVo(resumeSkillSearchVo, "skill_id asc");
		if (!resumeSkills.isEmpty()) {
			for (ResumeSkill resumeSkillItem : resumeSkills) {
				
				
				
				serviceTypeIds += resumeSkillItem.getSkillId() + ",";
				Dict dictSearchVo = new Dict();
				dictSearchVo.setId(resumeSkillItem.getSkillId());
				Dict dictRecord = dictService.selectOne(dictSearchVo);
				
				if (resumeSkillItem.getFirst().equals(1)) {
					resumeSkill = resumeSkillItem;
					serviceTypeId = resumeSkill.getSkillId();
					serviceTypeName = dictRecord.getName();
				}
				
				if (dictRecord != null) {
					serviceTypeNames += dictRecord.getName() + " ";
				}
				
				
			}
		}
		vo.setServiceTypeNames(serviceTypeNames);
		vo.setServiceTypeIds(serviceTypeIds);
		vo.setResumeSkill(resumeSkill);
		vo.setServiceTypeId(serviceTypeId);
		vo.setServiceTypeName(serviceTypeName);

		// 状态 1 = 空闲 0 = 上户
		String statusName = ResumeUtil.getResumeStatus(vo.getStatus());
		vo.setStatusName(statusName);
		
		// 判断是否有健康证
		// 判断是否有技能证
		
		Integer hasCertJianKang = 0;
		Integer hasCertSkill = 0;
		
		ResumeCert resumeCertSearchVo = new ResumeCert();
		resumeCertSearchVo.setResumeId(resumeId);
		List<ResumeCert> resumeCerts = resumeCertService.selectBySearchVo(resumeCertSearchVo, "id asc");
		for (ResumeCert resumeCert : resumeCerts) {
			if (resumeCert.getCertName().equals("健康证")) {
				hasCertJianKang = 1;
			} else {
				hasCertSkill = 1;
			}
		}
		vo.setHasCertJianKang(hasCertJianKang);
		vo.setHasCertSkill(hasCertSkill);
		
		//是否有身份证
		Integer hasIdCard = 0;
		resSearchVo = new ResumeResource();
		resSearchVo.setResumeId(resumeId);
		resSearchVo.setResourceType(Constants.RES_IDCARD_FRONT_IMG);
		List<ResumeResource> list = resumeResourceService.selectBySearchVo(resSearchVo, "add_time desc");
		if (list != null && list.size()>0) {
			hasIdCard = 1;
		}
		
		if (hasIdCard.equals(0)) {
			resSearchVo = new ResumeResource();
			resSearchVo.setResumeId(resumeId);
			resSearchVo.setResourceType(Constants.RES_IDCARD_BACK_IMG);
			list = resumeResourceService.selectBySearchVo(resSearchVo, "add_time desc");
			if (list != null && list.size()>0) {
				hasIdCard = 1;
			}
		}
		vo.setHasIdCard(hasIdCard);
						
		return vo;
	}

	@Override
	public ResumeDetailVo getDetailVo(Resume item) {
		ResumeListVo listVo = new ResumeListVo();
		listVo = this.getListVo(item);

		ResumeDetailVo vo = new ResumeDetailVo();
		BeanUtilsExp.copyPropertiesIgnoreNull(listVo, vo);

		Integer resumeId = vo.getResumeId();

		// 完整度百分比
		String totalPercent = String.valueOf(Constants.SCORE_DEFAULT + vo.getTotalComplete()) + "%";
		vo.setTotalPercent(totalPercent);

		// 添加时间格式化为yyyy-MM-dd HH:mm:ss
		Long addTime = Long.valueOf(vo.getAddTime() * 1000);
		String addTimeFormat = TimeStampUtil.timeStampToDateStr(addTime, DateUtil.DEFAULT_FULL_PATTERN);
		vo.setAddTimeFormat(addTimeFormat);

		// 排名
		Integer rowNum = 1;
		rowNum = this.selectByRowNum(resumeId);
		vo.setRowNum(rowNum);

		Integer total = 0;
		ResumeSearchVo searchVo = new ResumeSearchVo();
		total = this.selectCountBySearchVo(searchVo);
		vo.setTotal(total);

		// 民族名称
		String nationName = dictService.getDictName(vo.getNationId());
		vo.setNationName(nationName);

		// 学历名称
		String degreeName = dictService.getDictName(vo.getDegreeId());
		vo.setDegreeName(degreeName);

		
		//详细信息
		ResumeDetail resumeDetailSearchVo = new ResumeDetail();
		resumeDetailSearchVo.setResumeId(resumeId);
		ResumeDetail resumeDetail = resumeDetailService.selectOne(resumeDetailSearchVo);
		
		if (resumeDetail != null) {
			vo.setRemark(resumeDetail.getRemark());
			vo.setWorkHistory(resumeDetail.getWorkHistory());
			vo.setRateContent(resumeDetail.getRateContent());
			vo.setAddr(resumeDetail.getAddr());
		} else {
			vo.setRemark("");
			vo.setWorkHistory("");
			vo.setRateContent("");
			vo.setAddr("");
		}
		
		
		// 推荐人信息
		ResumeFrom resumeFromSearchVo = new ResumeFrom();
		resumeFromSearchVo.setResumeId(resumeId);
		ResumeFrom resumeFrom = resumeFromService.selectOne(resumeFromSearchVo);
		if (resumeFrom != null) {
			// 推荐人来源渠道
			String resumeFromName = ResumeUtil.getResumeFromName(resumeFrom.getResumeFrom());
			vo.setResumeFromName(resumeFromName);

			// 推荐人ID
			vo.setFromId(resumeFrom.getFromId());

			// 推荐人手机号
			vo.setFromMobile(resumeFrom.getFromMobile());

			// 推荐人名称
			vo.setFromName(resumeFrom.getFromName());

			// 推荐人评价
			vo.setFromRate(resumeFrom.getFromRate());

			// 推荐时间
			Long fromTime = Long.valueOf(resumeFrom.getAddTime() * 1000);
			String fromTimeFormat = TimeStampUtil.timeStampToDateStr(fromTime, DateUtil.DEFAULT_FULL_PATTERN);
			vo.setFromTimeFormat(fromTimeFormat);
		}

		// 服务区域
		Integer serviceCityId = 0;
		String regionIds = "";
		ResumeServiceArea resumeServiceAreaSearchVo = new ResumeServiceArea();
		resumeServiceAreaSearchVo.setResumeId(resumeId);
		List<ResumeServiceArea> resumeServiceAreas = resumeServiceAreaService
				.selectBySearchVo(resumeServiceAreaSearchVo, "region_id asc");
		List<ResumeServiceAreaVo> resumeServiceAreaVos = new ArrayList<ResumeServiceAreaVo>();
		for (ResumeServiceArea serviceArea : resumeServiceAreas) {
			serviceCityId = serviceArea.getCityId();
			regionIds += serviceArea.getRegionId().toString() + ",";

			ResumeServiceAreaVo resumeServiceAreaVo = new ResumeServiceAreaVo();
			BeanUtilsExp.copyPropertiesIgnoreNull(serviceArea, resumeServiceAreaVo);
			String provinceName = dictService.getDictName(resumeServiceAreaVo.getProvinceId());
			resumeServiceAreaVo.setProvinceName(provinceName);

			String cityName = dictService.getDictName(resumeServiceAreaVo.getCityId());
			resumeServiceAreaVo.setCityName(cityName);

			String regionName = dictService.getDictName(resumeServiceAreaVo.getRegionId());
			resumeServiceAreaVo.setRegionName(regionName);

			resumeServiceAreaVos.add(resumeServiceAreaVo);
		}
		vo.setServiceCityId(serviceCityId);
		vo.setRegionIds(regionIds);
		vo.setResumeServiceAreas(resumeServiceAreaVos);

		// 服务技能
		List<ResumeSkillVo> resumeSkillVos = new ArrayList<ResumeSkillVo>();
		String serviceTypeNames = "";
		ResumeSkill resumeSkillSearchVo = new ResumeSkill();
		resumeSkillSearchVo.setResumeId(resumeId);
		resumeSkillSearchVo.setFirst(0);
		List<ResumeSkill> resumeSkills = resumeSkillService.selectBySearchVo(resumeSkillSearchVo, "skill_id asc");
		if (!resumeSkills.isEmpty()) {
			for (ResumeSkill resumeSkill : resumeSkills) {
				ResumeSkillVo resumeSkillVo = new ResumeSkillVo();
				BeanUtilsExp.copyPropertiesIgnoreNull(resumeSkill, resumeSkillVo);
				String skillName = dictService.getDictName(resumeSkillVo.getSkillId());
				resumeSkillVo.setSkillName(skillName);
				resumeSkillVos.add(resumeSkillVo);

			}
		}
		vo.setResumeSkills(resumeSkillVos);

		// 证书信息
		ResumeCert resumeCertSearchVo = new ResumeCert();
		resumeCertSearchVo.setResumeId(resumeId);
		List<ResumeCert> resumeCerts = resumeCertService.selectBySearchVo(resumeCertSearchVo, "id asc");
		vo.setResumeCerts(resumeCerts);

		// 图片资源信息
		ResumeResource resumeResourceSearchVo = new ResumeResource();
		resumeResourceSearchVo.setResumeId(resumeId);
		List<ResumeResource> resumeResourceList = resumeResourceService.selectBySearchVo(resumeResourceSearchVo,
				"resource_id desc");
		if (resumeResourceList.isEmpty())
			resumeResourceList = new ArrayList<ResumeResource>();
		vo.setResumeResource(resumeResourceList);

		return vo;
	}

	/**
	 * 获取服务人员总数，最小值1567.
	 */
	@Override
	public Integer selectCountBySearchVo(ResumeSearchVo searchVo) {
		Integer total = resumeMapper.selectCountBySearchVo(searchVo);
		if (total == null)
			total = 0;
		total = 1567 + total;
		return total;
	}

	/**
	 * 计算排名
	 */
	@Override
	public Integer selectByRowNum(Integer resumeId) {
		return resumeMapper.selectByRowNum(resumeId);
	}

	/**
	 * 查询在特定地理位置，特定服务类型的人数
	 */
	@Override
	public Integer getServiceCount(Integer serviceTypeId, String lat, String lng) {
		// todo
		Integer total = 0;
		Random rand = new Random();
		total = rand.nextInt(10) + 20;
		return total;
	}
}
